const getDataStateHandle = function (target, name, descriptor) {
  let fn = descriptor.value
  descriptor.value = function () {
    let arg = Array.apply(null, arguments)
    let store = arg.slice(-1)[0]
    return new Promise(function (resolve, reject) {
      store.dispatch('setLoading', {visible: true})
      fn.apply(this, arg).then((data) => {
        store.dispatch('setLoading', {visible: false})
        resolve(data)
      }).catch((err) => {
        store.dispatch('setLoading', {visible: false})
        store.dispatch('setTip', {text: err.data ? err.data : err})
        reject(err)
      })
    })
  }
  return descriptor
}   // 请求状态处理

const microspotTarget = function (target, name, descriptor) {   // 埋点
  let fn = descriptor.value
  descriptor.value = function () {
    fn.apply(this, arguments).then((code) => {
      TDAPP.onEvent(code)
      TDAPP.send()
    })
  }
  return descriptor
}

export {
  getDataStateHandle,
  microspotTarget
}
